//
//  VisitorLoginView.swift
//  sinaWeibo
//
//  Created by liii on 16/6/26.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit
import SnapKit

// 定义协议, 遵守基协议
@objc protocol VisitorLoginDelegate: NSObjectProtocol {

    // 声明协议方法(optional声明可选协议, 默认都为required必须实现)
    func userWillLogin()
    optional func userWillRegistor()
}

class VisitorLoginView: UIView {
    
    // 设置代理属性(默认都是strong, 使用weak防止循环引用)
    weak var visitorDelegate: VisitorLoginDelegate?
    
    // 使用闭包实现按钮的点击(且为可选类型)
    var loginBtnClickBlock: (() -> ())?
    
    // MARK: - 外部方法
    func setVisitorWith(title: String, imageName: String?) {
        tipLabel.text = title
        
        if imageName != nil {
            circleImage.image = UIImage(named: imageName!)
            houseImage.hidden = true
            bringSubviewToFront(circleImage)
        } else {
            // 是首页控制器, 执行动画
            startAnimation()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        // 添加子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 旋转动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        anim.toValue = 2 * M_PI
        // 不移除动画
        anim.removedOnCompletion = false
        circleImage.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setupUI() {
        
        addSubview(circleImage)
        addSubview(maskImage)
        addSubview(houseImage)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // VFL
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        self.addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy:.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        // SnapKit
        circleImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        houseImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImage.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(80)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(30)
            make.width.equalTo(80)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(30)
            make.width.equalTo(80)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleImage.snp_center)
        }
        let value: CGFloat = 236 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: value)
        
        // 登陆按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    }
    
    @objc private func loginBtnClick() {
        // 闭包调用, 强制解包, 意味着必须实现
//        loginBtnClickBlock?()
        
        // 调用代理(必选方法)
        visitorDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnClick() {
        // 调用代理(可选方法)
        visitorDelegate?.userWillRegistor?()
    }

    // 懒加载子控件
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var tipLabel: UILabel = UIFactory.labelWithText("案例的开发及案件的法律的激发了京东方请我诶如案例的开始放假", fontSize: 14, textColor: UIColor.orangeColor(), alpha: 1)

    // 登陆按钮
    private lazy var loginBtn: UIButton = UIFactory.buttonWithTitle("登陆", fontSize: 14, color: UIColor.blackColor(), backgroundImage: "common_button_white_disable")
    // 注册按钮
    private lazy var registerBtn: UIButton = UIFactory.buttonWithTitle("注册", fontSize: 14, color: UIColor.blackColor(), backgroundImage: "common_button_white_disable")

//
//    {
//        let btn = UIButton()
//        btn.setTitle("登陆", forState: .Normal)
//        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
//        
//        /**
//            用代码拉伸图片
//            image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH)
//        */
////        let image = UIImage(named: "common_button_white_disable")!
////        let capW = Int(image.size.width * 0.5)
////        let capH = Int(image.size.height * 0.5)
////        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
//        btn.sizeToFit()
//        return btn
//    }()
    
}
